package com.heima.tlias.controller;

import com.heima.tlias.pojo.Dept;
import com.heima.tlias.service.DeptService;
import com.heima.tlias.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/depts")
@Slf4j
@Api(tags = "部门管理", value = "部门管理API")
public class DeptController {

    //Lombok构造方法注入
    private final DeptService deptService;

    /**
     * 接受客户端请求
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询部门列表")
    public Result<List<Dept>> deptList() {
        String threadName = Thread.currentThread().getName();
        log.info("DeptController 当前线程名称:{}", threadName);
        log.info("接收到客户端查询部门列表的请求");
        //调用业务层完成业务逻辑处理
        List<Dept> deptList = deptService.getDeptList();
        log.info("查询部门列表完成,列表数据:{}", deptList);
        log.info("参数:{}", deptList);
        return Result.success(deptList);
    }

    @DeleteMapping
    @ApiOperation("根据Id删除部门")
    public Result<String> deleteById(Integer id) {
        //System.out.println("接收到客户端的参数:" + id);
        log.info("接收到删除部门的请求,参数id:{}", id);
        //调用业务层删除数据
        deptService.deleteById(id);
        log.info("接收到删除部门的请求,参数id:{}", id);
        return Result.success("操作成功");
    }

    /**
     * 新增部门
     *
     * @param dept
     * @return
     */
    @PostMapping
    @ApiOperation("新增部门")
    public Result<String> insert(@RequestBody Dept dept) {
        log.info("接收到新增部门的请求,参数:{}", dept);
        //调用业务层新增数据
        deptService.insert(dept);
        log.info("接收到新增部门的请求,参数:{}", dept);
        return Result.success("新增部门成功");
    }

    /**
     * 根据id查询部门详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    @ApiOperation("根据Id查询部门详情")
    public Result<Dept> queryById(@PathVariable("id") Integer id) {
        log.info("接收到查询部门详情的请求,参数id:{}", id);
        Dept dept = deptService.queryById(id);
        log.info("查询部门详情成功,详情数据:{}", dept);
        return Result.success(dept);
    }

    /**
     * 修改部门信息
     *
     * @param dept
     * @return
     */
    @PutMapping
    @ApiOperation("更新部门")
    public Result<String> updateById(@RequestBody Dept dept) {
        log.info("接收到修改部门的请求,参数:{}", dept);
        deptService.updateById(dept);
        log.info("修改部门成功");
        return Result.success("修改数据成功");
    }

}
